<template>
  <van-cell-group>
    <van-cell
      disabled
      title="候选人"
      style="width: 30%; float: left;"
    />
    <van-picker
      :columns="candidates_name"
      @change="onChange"
      style="width: 70%; float: right;"
    />
    <van-button size="large" style="float: right;" @click="deleteCandid">删除</van-button>
    <div style="clear: both; height: 0;"></div>
  </van-cell-group>
</template>

<script>
  import {Cell, CellGroup, Picker, Button, Toast, Dialog} from 'vant';

  export default {
    name: "DelCandidates",
    components: {
      [Cell.name]: Cell,
      [CellGroup.name]: CellGroup,
      [Picker.name]: Picker,
      [Button.name]: Button,
      [Toast.name]: Toast,
      [Dialog.name]: Dialog,
    },
    data(){
      return{
        candidates_id: [],
        candidates_name: [],
        candid: 0
      }
    },
    methods:{
      deleteCandid() {
        let _this = this;
        Dialog.confirm({
          title: '您确定要删除吗？',
        }).then(() => {
          let token = _this.getCookie("token");
          let data = _this.$qs.stringify({
            'id': _this.candidates_id[_this.candid]
          });
          _this.$http.delete(_this.apiUrl('user/selection/delCandidate'), {
            data: data,
            headers: {
              'token': token,
              'sid': _this.$route.query.sid
            }
          }).then((res) => {
            // console.log(res.data);
            if(res.data.statusCode === 200){
              _this.candidates_id.splice(_this.candid, 1);
              _this.candidates_name.splice(_this.candid, 1);
            }
          });
        }).catch(() => {
        });
      },
      getData() {
        let _this = this;
        _this.$http.get(_this.apiUrl("selection/index/getCandidateAll"), {
          headers: {
            'sid': _this.$route.query.sid
          }
        }).then((res) => {
          // console.log(res.data);
          let candidates_list = res.data.data.candidates;
          for (let i = 0; i < candidates_list.length; i++) {
            _this.candidates_id.push(candidates_list[i].id);
            _this.candidates_name.push(candidates_list[i].name);
          }
        });
      },
      onChange(Picker, value, index){
        let _this = this;
        _this.candid = index;
      }
    },
    mounted(){
      let _this = this;
      _this.getData();
    }
  }
</script>

<style scoped>

</style>
